package com.core.base;

import com.core.model.PageData;
import com.core.response.ResponseResult;
import com.model.jpa.Users;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.beans.IntrospectionException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;

public abstract class BaseController<T, ID extends Serializable> implements GetService<T, ID> {


    private final Logger logger = LoggerFactory.getLogger(BaseController.class);

    public static final String PARAM_ERROR = "参数错误";

    public static final String SYS_ERROR = "后台错误";


    @PostMapping(value = "form")
    public ResponseResult formPost(String id, @RequestBody T t) throws IllegalAccessException, IntrospectionException, InvocationTargetException {
        logger.info(": 进入formPost");

        if (t == null) {
            return ResponseResult.error(PARAM_ERROR);
        }
        int result = 0;

        if (id == null) {
            result = getService().add(t);
        } else {
            result = getService().update(t);
        }

        if (result > 0) {
            return ResponseResult.error(SYS_ERROR);
        }
        return ResponseResult.success();
    }

    @GetMapping(value = "delete")
    public ResponseResult delete(String idList) {
        if (idList == null || idList.length() <= 0) {
            return ResponseResult.error(PARAM_ERROR);
        }
        int result = getService().deleteById(idList);
        if (result > 0) {
            return ResponseResult.error(SYS_ERROR);
        }


        return ResponseResult.success();
    }


    @GetMapping(value = "logicDelete")
    public ResponseResult logicDelete(String idList) throws IllegalAccessException, IntrospectionException, InvocationTargetException {
        if (idList == null || idList.length() <= 0) {
            return ResponseResult.error(PARAM_ERROR);
        }
        int result = getService().logicDelete(idList);
        if (result > 0) {
            return ResponseResult.error(SYS_ERROR);
        }


        return ResponseResult.success();
    }

    @GetMapping(value = "get")
    public ResponseResult get(ID id) throws IllegalAccessException, IntrospectionException, InvocationTargetException {
        if (id == null) {
            return ResponseResult.error(PARAM_ERROR);
        }

        T t = getService().getById(id);
        if (t == null) {
            return ResponseResult.success();
        }
        return ResponseResult.success(t);

    }

    @GetMapping(value = "getAll")
    public ResponseResult getAll(HttpServletRequest request) {
        PageData pageData = new PageData(request);
        if (!pageData.containsKey("isPage")) {
            return ResponseResult.success(getService().getAll());
        }


        Integer pagesize = pageData.getRows();
        Integer page = pageData.getPageIndex() - 1;
        Pageable pageable = null;
        if (pagesize != null && page >= 0) {
            Sort sort = new Sort(Sort.Direction.DESC, "id");
            pageable = PageRequest.of(page, pagesize, sort);
        }
        if (pageable == null) {
            return ResponseResult.error(PARAM_ERROR);
        }
        return ResponseResult.success(getService().getPage(pageable));

    }


}
